package com.xzy.f;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.function.Supplier;

/**
 * 说明：通过Supplier定义Bean
 *
 * @author xzy
 * @date 2021/11/28 21:20
 */
public class Main {
    public static void main(String[] args) {
        method0();
        method1();
    }

    public static void method0() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.registerBean(Cat.class);
        applicationContext.refresh();

        // Cat的属性都是空的
        Cat cat = applicationContext.getBean(Cat.class);
        System.out.println(cat);
    }

    public static void method1() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        applicationContext.registerBean(Cat.class, new Supplier<Cat>() {
            @Override
            public Cat get() {
                // 自定义创建Cat的过程
                Cat cat = new Cat();
                cat.setName("Tom");
                return cat;
            }
        });
        applicationContext.refresh();

        // 由于自定义了填充Cat的过程，因此Cat的属性不是空的
        Cat cat = applicationContext.getBean(Cat.class);
        System.out.println(cat);
    }
}
